#include <iostream>
#include <functional>

void save_with_free_fun(int a, int b)
{
    std::cout << "a + b = " << a + b << std::endl;
}

class FileSave
{
private:
    // std::string filename;
public:
    // 使用编译器生成的默认构造函数
    FileSave() = default;
    // 使用编译器生成的默认析构函数
    ~FileSave() = default;
    void save_with_member_fun(int a, int b)
    {
        std::cout << "a + b = " << a + b << std::endl;
    }
};

int main()
{
    
    FileSave fs;

    auto save_with_free_fun_lambda = [](int a, int b) -> void
    { std::cout << "a + b = " << a + b << std::endl; };

    // std::function<int(int, int)> add = [](int a, int b) -> int
    // { return a + b; };

    save_with_free_fun(1, 2);
    fs.save_with_member_fun(1, 2);
    save_with_free_fun_lambda(1, 2);

    std::function<void(int, int)> save1 = save_with_free_fun;
    // std::function<void(int,int)> save2 = fs.save_with_member_fun; 错误，成员函数不能直接复制给其他
    std::function<void(int, int)> save2 = std::bind(&FileSave::save_with_member_fun, &fs,
                                                    std::placeholders::_1, std::placeholders::_2);
    std::function<void(int, int)> save3 = save_with_free_fun_lambda;

    save1(1, 2);
    save2(1, 2);
    save3(1, 2);
    return 0;
}